package com.party.controller.sys.apply;

import com.party.common.IdcardValidator;
import com.party.common.Page;
import com.party.common.Tools;
import com.party.model.*;
import com.party.service.apply.ApplyService;
import com.party.service.study.Curriculum2contentService;
import com.party.service.study.CurriculumService;
import com.party.service.study.LearningContentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *活动申请controller
 */
@Controller
@RequestMapping("/sys/apply")
public class ApplyController
{
    @Resource
    ApplyService applyService;

    private int pageSize = 9;

    /**
     *  列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(
            HttpServletRequest request,
            HttpServletResponse response
    ) {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/apply/apply");
        return modelAndView;
    }

    @RequestMapping(value = "/query")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        String pageIndexStr = req.getParameter("pageIndex");
        String content = req.getParameter("content");
        String title = req.getParameter("title");
        String status = req.getParameter("status");//-1未通过，0未审批，1通过
        String realname = req.getParameter("realname");//申请人名称

        int pageIndex = 1;
        if (pageIndexStr != null && !"".equals(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }
        Apply apply = new Apply();
        apply.setTitle(title);
        apply.setRealname(realname);
        apply.setStatus(status);
        apply.setContent(content);


        Page p = this.applyService.getList(apply, pageSize, pageIndex);

        map.put("pageCount",p.getTotalPageCount() );
        map.put("tableData",p.getResult());
        return map;
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String,Object> delete(HttpServletRequest req) {
        String ids = req.getParameter("ids");
        int num = 0;
        if(ids != null && !"".equals(ids))
        {
            String[] idStrs = ids.split(",");
            if(idStrs.length>0)
            {
                for(String id : idStrs)
                {
                    if(Tools.isNumber(id))
                    {
                        num = num+this.applyService.delete(Integer.parseInt(id));
                    }
                }
            }
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("num",num);
        return map;
    }


    /**
     * 增加
     * @return
     */
    @RequestMapping(value = "/add")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/apply/apply_add");
        return modelAndView;
    }

    /**
     * 增加数据
     * @return
     */
    @RequestMapping(value = "/addData")
    @ResponseBody
    public Map<String,Object> addData(
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        String status="";
        if ( session.getAttribute("sysUser") != null) {
            String title = request.getParameter("title");
            String timeBegin = request.getParameter("timeBegin");
            String timeEnd = request.getParameter("timeEnd");
            String moneyStr = request.getParameter("money");
            String content = request.getParameter("content");
            String meaning = request.getParameter("meaning");
            String realname = request.getParameter("realname");

            Apply apply = new Apply();
            if(moneyStr != null && !"".equals(moneyStr) && Tools.isNumber(moneyStr))
            {
                apply.setMoney(Integer.parseInt(moneyStr));
            }
            else
            {
                apply.setMoney(0);//默认
            }

            SysUser user = ((SysUser) session.getAttribute("sysUser"));
            apply.setUid(user.getUid());
            apply.setRealname(user.getRealname());
            apply.setStatus("0");//默认未审核
            apply.setContent(content);
            apply.setMeaning(meaning);
            apply.setTimebegin(timeBegin);
            apply.setTimeend(timeEnd);
            apply.setTitle(title);
            String time=Tools.getNowDate();
            apply.setTime(time);
            int num=0;
            num = this.applyService.add(apply);
            if(num>0){
                status="1";
            }else{
                status="0";
            }
        }
        else
        {
            status="1";
        }
        map.put("status",status);
        return map;
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        String id=request.getParameter("id");
        String status="0";
        Apply apply=null;
        if(id != null && !"".equals(id) && Tools.isNumber(id)) {
            int intId = Integer.parseInt(id);
            apply = applyService.getById(intId);
            if("1".equals(apply.getStatus()))
            {
                //已经审核成功，不能编辑
                modelAndView.setViewName("redirect:/sys/apply/index.do");
                return modelAndView;
            }
            else
            {
                if(apply!=null){
                    status="1";
                }
            }
        }

        modelAndView.addObject("obj",apply);
        modelAndView.addObject("status",status);
        modelAndView.setViewName("/sys/apply/apply_edit");
        return modelAndView;
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping(value = "/editData")
    @ResponseBody
    public Map<String,Object> editData(
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response) {
        String status="0";
        int num= 0;
        String idStr = request.getParameter("id");
        if(idStr != null && !"".equals(idStr) && Tools.isNumber(idStr))
        {
            Apply apply = this.applyService.getById(Integer.parseInt(idStr));
            if(!apply.getStatus().equals("1"))
            {
                String title = request.getParameter("title");
                String content = request.getParameter("content");
                String meaning = request.getParameter("meaning");
                String timeBegin = request.getParameter("timeBegin");
                String timeEnd = request.getParameter("timeEnd");
                String moneyStr = request.getParameter("money");

                apply.setContent(content);
                apply.setMeaning(meaning);
                apply.setTimebegin(timeBegin);
                apply.setTimeend(timeEnd);
                apply.setTitle(title);
                if(moneyStr != null && !"".equals(moneyStr) && Tools.isNumber(moneyStr))
                {
                    apply.setMoney(Integer.parseInt(moneyStr));
                }
                else
                {
                    apply.setMoney(0);//默认
                }
                num = this.applyService.edit(apply);
                if(num>0){
                    status="1";
                }else{
                    status="0";
                }
            }
            else
            {
                status = "-1";//已经审核成功了，不能编辑
            }
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status",status);

        return map;
    }

/////////////////////////////////////////
    //审核的请求响应
    //////////////
    /**
     *  列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/checkIndex", method = RequestMethod.GET)
    public ModelAndView checkIndex(
            HttpServletRequest request,
            HttpServletResponse response
    ) {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/apply/checkApply");
        return modelAndView;
    }

    /**
     *  列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/check", method = RequestMethod.GET)
    public ModelAndView check(
            HttpServletRequest request,
            HttpServletResponse response
    ) {

        ModelAndView modelAndView = new ModelAndView();
        String id=request.getParameter("id");
        String status="0";
        Apply apply=null;
        if(id != null && !"".equals(id) && Tools.isNumber(id)) {
            int intId = Integer.parseInt(id);
            apply = applyService.getById(intId);
            if("1".equals(apply.getStatus()))
            {
                //已经审核成功，不能编辑
                modelAndView.setViewName("redirect:/sys/apply/index.do");
                return modelAndView;
            }
            else
            {
                if(apply!=null){
                    status="1";
                }
            }
        }

        modelAndView.addObject("obj",apply);
        modelAndView.addObject("status",status);
        modelAndView.setViewName("/sys/apply/check");
        return modelAndView;
    }
    /**
     * 审核
     * @return
     */
    @RequestMapping(value = "/checkDo")
    @ResponseBody
    public Map<String,Object> checkDo(
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response) {
        String status="";
        int num= 0;
        String idStr = request.getParameter("id");
        if ( session.getAttribute("sysUser") != null
                && idStr != null && !"".equals(idStr) && Tools.isNumber(idStr)) {
            SysUser user = (SysUser)session.getAttribute("sysUser");
            Apply apply = this.applyService.getById(Integer.parseInt(idStr));
            String statusStr = request.getParameter("status");
            String checkMsg = request.getParameter("checkMsg");//审批说明
            if(statusStr == null || "".equals(statusStr))
            {
                statusStr = "0";
            }
            apply.setStatus(statusStr);
            apply.setCheckmsg(checkMsg);
            apply.setCheckuid(user.getUid());
            apply.setChecktime(Tools.getNowDate());
            num = this.applyService.edit(apply);
        }

        if(num>0){
            status="1";
        }else{
            status="0";
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status",status);

        return map;
    }




}
